<?php
// 插入历史记录
function insertHistory($conn, $userId, $goodId) {
    $sql = "INSERT INTO history (user_id, good_id, creator, is_deleted, update_time, create_time) VALUES (?, ?, ?, ?, ?, ?)";
    $stmt = $conn->prepare($sql);
    $stmt->execute([
        $userId,
        $goodId,
        getUserNameById($conn, $userId),
        0,
        date('Y-m-d H:i:s'),
        date('Y-m-d H:i:s')
    ]);
}

// 插入收藏记录
function insertCollect($conn, $userId, $goodId) {
    $sql = "INSERT INTO collect (good_id, user_id, creator, is_deleted, create_time, update_time) VALUES (?, ?, ?, ?, ?, ?)";
    $stmt = $conn->prepare($sql);
    $stmt->execute([
        $goodId,
        $userId,
        getUserNameById($conn, $userId),
        0,
        date('Y-m-d H:i:s'),
        date('Y-m-d H:i:s')
    ]);
}

// 实现点赞功能
function likeProduct($conn, $userId, $goodId, $status) {
    $sql = "INSERT INTO like (user_id, good_id, creator, is_deleted, update_time, create_time,status) VALUES (?, ?, ?, ?, ?, ?,?)";
    $stmt = $conn->prepare($sql);
    $stmt->execute([
        $userId,
        $goodId,
        getUserNameById($conn, $userId),
        $status,
        date('Y-m-d H:i:s'),
        date('Y-m-d H:i:s')
    ]);
    return $stmt->rowCount() > 0;
}

// 添加评论
function addComment($conn, $userId, $goodId, $content) {
    $sql = "INSERT INTO comment (good_id, content, user_id, update_time, create_time, is_deleted, creator) VALUES (?, ?, ?, ?, ?, ?, ?)";
    $stmt = $conn->prepare($sql);
    $stmt->execute([
        $goodId,
        $content,
        $userId,
        date('Y-m-d H:i:s'),
        date('Y-m-d H:i:s'),
        0,
        getUserNameById($conn, $userId)
    ]);
}

// 根据用户ID和商品ID获取评论
function getCommentsByUserAndGood($conn, $userId, $goodId) {
    $sql = "SELECT * FROM comment WHERE user_id = ? AND good_id = ? AND is_deleted = 0";
    $stmt = $conn->prepare($sql);
    $stmt->execute([$userId, $goodId]);
    return $stmt->fetchAll();
}

// 根据用户ID和商品ID获取点赞信息
function getLikeByUserAndGood($conn, $userId, $goodId) {
    $sql = "SELECT * FROM like WHERE user_id = ? AND good_id = ? AND is_deleted = 0";
    $stmt = $conn->prepare($sql);
    $stmt->execute([$userId, $goodId]);
    return $stmt->fetch();
}

// 根据用户ID和商品ID获取收藏信息
function getCollectByUserAndGood($conn, $userId, $goodId) {
    $sql = "SELECT * FROM collect WHERE user_id = ? AND good_id = ? AND is_deleted = 0";
    $stmt = $conn->prepare($sql);
    $stmt->execute([$userId, $goodId]);
    return $stmt->fetch();
}

// 根据用户ID和商品ID删除收藏信息
function deleteCollectByUserAndGood($conn, $userId, $goodId) {
    $sql = "UPDATE collect SET is_deleted = 1 WHERE user_id = ? AND good_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->execute([$userId, $goodId]);
    return $stmt->rowCount() > 0;
}

// 根据用户ID和商品ID删除历史记录信息
function deleteHistoryByUserAndGood($conn, $userId, $goodId) {
    $sql = "UPDATE history SET is_deleted = 1 WHERE user_id = ? AND good_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->execute([$userId, $goodId]);
    return $stmt->rowCount() > 0;
}

// 根据用户ID和商品ID修改状态信息
function updateStatusByUserAndGood($conn, $userId, $goodId, $status) {
    $sql = "UPDATE like SET is_deleted = ? WHERE user_id = ? AND good_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->execute([$status, $userId, $goodId]);
    return $stmt->rowCount() > 0;
}

// 通过id获取用户名
function getUserNameById($conn, $userId) {
    $sql = "SELECT uname FROM user WHERE id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->execute([$userId]);
    return $stmt->fetch()['uname'] ?? null;
}